<?php

namespace api\modules\v1\models;

use common\models\member\Superior;
use Yii;

class ModuleSuperior
{


    /**
     * 设置用户数据
     */
    public static function getUserJDData(&$item,$member_id = 0){
        //TODO:根据用户等级获取不同比例
        $bili = 0.5;
        if($item['commissionInfo'] && $item['commissionInfo']['commission']){
            $item['commissionInfo']['commission'] = round($item['commissionInfo']['commission']*$bili,2);
        }
    }

    /**
     * 插入级别系统
     * @param $member_id
     * @param $code
     * @return bool
     */
    public static function createSuperior($member_id, $code)
    {
        $up = base64_decode($code);
        if ($up) {
            $up = @json_decode($up, true);
            if (isset($up['member_id']) && $up['member_id'] > 0) {
                //检查是否存在
                $my_superior = Superior::findOne(['member_id' => $member_id]);
                $up_superior = Superior::findOne(['member_id' => $up['member_id']]);//上级数据
                if (!$up_superior) {
                    $SuperiorModel = new Superior();
                    $SuperiorModel->member_id = $up['member_id'];
                    $SuperiorModel->superior1 = 0;
                    $SuperiorModel->superior2 = 0;
                    $SuperiorModel->superior3 = 0;
                    $SuperiorModel->save();
                    $up_superior = Superior::findOne(['member_id' => $up['member_id']]);//上级数据
                }
                if (!$my_superior) {
                    $SuperiorModel = new Superior();
                    $SuperiorModel->member_id = $member_id;
                    $SuperiorModel->superior1 = $up['member_id'];
                    $SuperiorModel->superior2 = $up_superior ? $up_superior['superior1'] : 0;
                    $SuperiorModel->superior3 = $up_superior ? $up_superior['superior2'] : 0;
                    $SuperiorModel->save();
                }
                //TODO:更新粉丝数量
                $fans_count = Superior::find()->where(['superior1'=>$up['member_id']])->count();
                $fans_total = Superior::find()->where(['OR','superior2'=>$up['member_id'],'superior2'=>$up['member_id']])->count();
                $up_superior->fans_count = $fans_count;
                $up_superior->fans_total = $fans_total;
                $up_superior->save();
            }
        }
        return false;
    }
}